        var packingStore = new Ext.data.JsonStore({
    		url: '/weihe/getItems_packingList',
			root: 'datas',
			fields : [{
			        	   name:'packingId',mapping:'id'
			          },{
			        	   name:'packingAmount',mapping:'amount' 
			          },{
        				   name:'packingPrice',mapping:'price'
        			  },{
        				   name:'packingSum',mapping:'sum'
        			  },{
        				   name:'packingCode',mapping:'code'
        			  },{
        				   name:'packingRemark',mapping:'remark'
        			  },{
       				   	   name:'cargoId',mapping:'cargo', convert:function(v){ 
																return v ? v.id : undefined;}
        			  },{
        				   name:'packingName',mapping:'cargo', convert:function(v){ 
        				  										return v ? v.name : undefined;}
        			  },{
        			  	   name:'packingSpecs',mapping:'cargo', convert:function(v){ 
        			  						                    return v ? v.specs : undefined;}
        			  },{
        				   name:'packingOrigin',mapping:'cargo', convert:function(v){ 
		                                                        return v ? v.origin : undefined;}
        			  },{
        				   name:'packingUnit',mapping:'cargo', convert:function(v){ 
        				  		                                 return v ? v.unit : undefined;}
        			    }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_packingList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}
		});
        
        var packingListStore = new Ext.data.JsonStore({
			url: '/weihe/getList_packingList',
			root: 'datas',
			fields : [{
			        	   name:'packingListId',mapping:'id'
			          },{
			        	   name:'packingListCode',mapping:'code' 
			          },{
        				   name:'packingSeal',mapping:'seal'
        			  },{
        				   name:'packingDate', mapping:'date',type : 'date', dateFormat:"Y-m-d"
        			  },{
        				   name:'packingTotal',mapping:'total'
        			  },{
        				   name:'packingWeight',mapping:'weight'
        			  }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_packingList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}
		});
        
       
        
packings = function(){
	
	var tmp='';

	
  return{
	  
	  
		listPanel : function(){
	  
		var listPanel = new Ext.Panel( {
			                    region : 'north',
			                    layout : 'border',
			                    frame  : true,
			                    height :250,
			                    items  : [packings.listGrid(),packings.listForm()] 
		                      });
	  
	  return listPanel;
},

listForm : function(){
	  
    


		var button_add = new Ext.Button({
			text : '添加',
			handler : function() {
				progressConsole();
				if(listForm.form.isValid()){ 
					Ext.getCmp("packingsForm").buttons[0].enable();
					Ext.getCmp("packingsForm").buttons[1].enable();
					Ext.getCmp("packingsForm").buttons[2].enable();
					if (listForm.form.findField("packingListId").getValue() == "") {
						 listForm.form.submit({
						     url : '/weihe/add_packingList',
						     success : function(form, action) {
							      if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
								        Ext.Msg.alert('消息', action.result.msg, function() {
								        Ext.getCmp("packingListForm").form.findField('packingList.id').setValue(action.result.packingList_id);
									    Ext.getCmp("packingsForm").form.findField('packingList.id').setValue(action.result.packingList_id);
									    Ext.getCmp("packingListGrid").getStore().load({params : {start : action.result.total-1, limit : 5}});
									    listForm.buttons[0].setText('修改');
								        })
							       }
						     }
					      })
					}else {
							listForm.form.submit({
								url : '/weihe/update_packingList',
								success : function(form, action) {
									if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
										Ext.Msg.alert('消息', action.result.msg, function() {
											Ext.getCmp("packingListGrid").getStore().load({params : {start : 0, limit : 5}});
										})
									}
							   }
							})
					}
				}else{
					Ext.Msg.alert('提示','请输入相关内容');
				}
			}
		});

	var button_clear = new Ext.Button({
		text : '清空',
		handler : function() {
		
			listForm.form.reset();
			listForm.buttons[0].setText('添加');
			Ext.getCmp("packingsForm").form.reset();
			Ext.getCmp("packingsForm").buttons[0].disable();
		    Ext.getCmp("packingsForm").buttons[1].disable();
		    Ext.getCmp("packingsForm").buttons[2].disable();
		    Ext.getCmp("packingGrid").getStore().removeAll();
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		handler : function() {
			var id = listForm.form.findField('packingListId').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/delete_packingList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
						  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									listForm.form.reset();
								listForm.buttons[0].setText('添加');
						  }else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("packingListGrid").getStore().load({params : {start : 0, limit : 5}});
								listForm.form.reset();
								listForm.buttons[0].setText('添加');
							});
						  }
						}else{
							Ext.Msg.alert('消息', json.msg);
						}
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params: { 'packingList.id' : listForm.form.findField('packingListId').getValue() }
				});
			}
		}
	});
	
	  
			var listForm = new Ext.form.FormPanel({
			id : 'packingListForm',
			region : 'east',
			title : '装箱清单信息',
			frame : true,
			width : 300,
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{
				id: 'packingListId',
				xtype : 'hidden',
				name : 'packingList.id'
			},{
				id: 'packingListCode',
				fieldLabel: "柜号",
				name: "packingList.code"
			},{
				id: 'packingDate',
                xtype: 'datefield',
                fieldLabel: "装箱日期", 
                format : 'Y-m-d',
    			allowBlank:false,
                name: "packingList.date"
			},{
				id: 'packingSeal',
				fieldLabel: "进港封条",
          	    name: "packingList.seal"
			},{
				id: 'packingWeight',
				 fieldLabel: "货柜重量",
                 name: "packingList.weight"
			}],
			buttons : [button_add,button_clear,button_del]
	});
	  
	  return listForm;
},  
	  
	  
  listGrid : function(){
	  
		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 5,
			store : packingListStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		   
	   var listGrid = new Ext.grid.GridPanel({
		   id:'packingListGrid',
		   title : 'packing信息表',
		   region : 'center',
		   minColumnWidth : 35,
		   loadMask : {msg:'正在加载数据，请稍侯...'},
		   tbar:[{
			  	text:"打印装箱清单",
				handler:function(){
							if(tmp!=''){
			   				window.open('/weihe/printPackingList?packingList.id='+ tmp, '_blank');
			   				tmp = '';
							}else{
								Ext.Msg.alert('提示','请选择要打印的数据!');
							}
				}
						
		   		},"-"],
		   store : packingListStore,
		   columns: [
		             {header : '柜号', dataIndex : 'packingListCode'},
		             {header : '装箱日期', dataIndex : 'packingDate',sortable : true,renderer:Ext.util.Format.dateRenderer('Y年m月d日')},
		             {header : '进港封条', dataIndex : 'packingSeal', menuDisabled : true},
		             {header : '总价', dataIndex : 'packingTotal', menuDisabled : true},
		             {header : '货柜重量', dataIndex : 'packingWeight', menuDisabled : true}
		             ],
         sm : rowSelectionModel,
         listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   		var record = grid.getStore().getAt(rowIndex);
			   		tmp = record.id;
			   		Ext.getCmp("packingListForm").form.loadRecord(record);
			   		Ext.getCmp("packingsForm").form.findField('packingList.id').setValue(record.id);
			   		Ext.getCmp("packingsForm").buttons[0].enable();
					Ext.getCmp("packingsForm").buttons[1].enable();
					Ext.getCmp("packingsForm").buttons[2].enable();
					Ext.getCmp("packingListForm").buttons[0].setText('修改');
					Ext.getCmp("packingGrid").getStore().baseParams = {'packingList.id': record.id};
					Ext.getCmp("packingGrid").getStore().load({params : {start : 0, limit : 5}});
		   		}
		   },
		  bbar : pageToolBar
});
		return listGrid;
	},
  
    grid : function(){
	 


		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 6,
			store : packingStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		   


		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		   
	   var grid = new Ext.grid.GridPanel({
		   id:'packingGrid',
		   title : '货物信息表',
		   region : 'center',
		   minColumnWidth : 50,
		   loadMask : {msg:'正在加载数据，请稍侯...'}, // 单击分页时显示,为true显示'Loading...'
		   store : packingStore,
		   columns: [
		             	{header : "序号",renderer:function(value,metadata,record,rowIndex, colIndex, store){
		            	 															var start = store.lastOptions.params.start;
		            	 															if (isNaN(start)) {
		            	 																start = 0;}
		            	 															return start + rowIndex + 1;}},
		             	{header : '编码', dataIndex : 'packingCode', menuDisabled : true},
		             	{header : '名称', dataIndex : 'packingName'},
		             	{header : '规格', dataIndex : 'packingSpecs', menuDisabled : true},
		             	{header : '产地', dataIndex : 'packingOrigin', menuDisabled : true},
		             	{header : '数量', dataIndex : 'packingAmount', menuDisabled : true},
		             	{header : '单位', dataIndex : 'packingUnit', menuDisabled : true},
		             	{header : '单价', dataIndex : 'packingPrice',sortable : true},
		             	{header : '金额', dataIndex : 'packingSum',sortable : true},
		             	{header : '备注', dataIndex : 'packingRemark', menuDisabled : true}

        ],
           sm : rowSelectionModel,
           listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   var record = grid.getStore().getAt(rowIndex);
			   Ext.getCmp("packingsForm").form.loadRecord(record);
			   Ext.getCmp("packingsForm").buttons[0].setText('修改');
			  
		   }
		},
		  viewConfig : {
		     forceFit : false
		  },
		  bbar : pageToolBar
});
		return grid;
	},
	packingsForm : function(){

		
		var namestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByname_shoppingList',
			root: 'names',
			totalProperty: 'count',
			fields: ['name']
		});
		

		var nameCombo = {
				id : 'packingName',
				xtype: 'combo',
				fieldLabel: '名称',
				store: namestore,
				typeAhead: false,
				name : 'cargo.name',
				loadingText: '查找中...',
				emptyText: '请输入产品名称', 
				minChars: 2,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'name',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getName_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  packingsForm.form.findField('packingSpecs').setValue(json.specs);
									  packingsForm.form.findField('packingUnit').setValue(json.unit);
									  packingsForm.form.findField('packingPrice').setValue(json.price);
									  packingsForm.form.findField('cargo.id').setValue(json.id);
									},
								params : 'query=' + record.get('name')
							});
				    }
				}
		};
		
		var button_add = new Ext.Button({
			text : '添加',
			disabled : true,
			handler : function() {

					progressConsole();
				if(packingsForm.form.isValid()){ 	
					if (packingsForm.form.findField("packingItem.id").getValue() == "") {
						packingsForm.form.submit({
						  url : '/weihe/addItem_packingList',
						   success : function(form, action) {
							  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
							  }else{
								Ext.Msg.alert('消息', action.result.msg, function() {
									 var i = packingsForm.form.findField('packingList.id').getValue();
									   packingsForm.form.reset();
									   packingsForm.form.findField('packingList.id').setValue(i);
									   Ext.getCmp("packingGrid").getStore().baseParams = {'packingList.id': i};
									   Ext.getCmp("packingGrid").getStore().load({params : {start : 0, limit : 6}});
									   Ext.getCmp("packingListGrid").getStore().reload();
								})
							  }
						    },
						    failure : function(form, action) {
      							Ext.Msg.alert('错误', action.result.msg);
      						}
					    })
					}else {
						packingsForm.form.submit({
							  url : '/weihe/updateItem_packingList',
							   success : function(form, action) {
								  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
									Ext.Msg.alert('消息', action.result.msg, function() {
										var i = packingsForm.form.findField('packingList.id').getValue();
										packingsForm.form.reset();
										 packingsForm.form.findField('packingList.id').setValue(i);
										 Ext.getCmp("packingGrid").getStore().reload();
										 Ext.getCmp("packingListGrid").getStore().reload();
										 packingsForm.buttons[0].setText('添加');
										
									})
								  }
							    },
							    failure : function(form, action) {
	      							Ext.Msg.alert('错误', action.result.msg);
	      						}
						    })

				
						
					}
			}else{
				Ext.Msg.alert('提示','请输入相关内容');
			}
				}
		
			
		});

	var button_clear = new Ext.Button({
		text : '清空',
		disabled : true,
		handler : function() {
		var i = packingsForm.form.findField('packingList.id').getValue();
			packingsForm.form.reset();
			packingsForm.form.findField('packingList.id').setValue(i);
		    packingsForm.buttons[0].setText('添加');
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		disabled : true,
		handler : function() {
			var id = packingsForm.form.findField('packingItem.id').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/deleteItem_packingList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
						  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
								var i = packingsForm.form.findField('packingList.id').getValue();
								packingsForm.form.reset();
								packingsForm.form.findField('packingList.id').setValue(i);
								packingsForm.buttons[0].setText('添加');
						  }else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("packingGrid").getStore().load({params : {start : 0, limit :6}});
								var i = packingsForm.form.findField('packingList.id').getValue();
								packingsForm.form.reset();
								packingsForm.form.findField('packingList.id').setValue(i);
								packingsForm.buttons[0].setText('添加');
								Ext.getCmp("packingGrid").getStore().reload();
								Ext.getCmp("packingListGrid").getStore().reload();
							});
						  }
						}
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params: { 'packingItem.id': id , 'packingList.id' : packingsForm.form.findField('packingList.id').getValue() }

				});
			}
		}
	});
	
		var packingsForm = new Ext.form.FormPanel({
			id : 'packingsForm',
			title : '货物信息',
			frame : true,
			width : 300,
			region : 'east',
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{ 
				id: 'packingId',
				xtype : 'hidden', 
				name : 'packingItem.id'
			},{ 
				xtype : 'hidden', 
				name : 'packingList.id'
			},{
				xtype : 'hidden', 
				name : 'cargo.id'
			},{
				id:'packingCode',
				fieldLabel : '编码',
				name : 'packingItem.code'
			},nameCombo
			 ,{
				id:'packingSpecs',
				fieldLabel : '规格',
				name : 'cargo.specs',
				readOnly: true
			},{
				id:'packingUnit',
				fieldLabel : '单位',
				name : 'cargo.unit',
				readOnly: true
			},{
				id:'packingPrice',
				fieldLabel : '单价',
				name : 'packingItem.price'
			},{
				id:'packingAmount',
				fieldLabel : '数量',
				name : 'packingItem.amount',
				regex : /^[1-9]\d*$/,
			    regexText:'请输入有效的数量',  
			    allowBlank:false,
				blankText:'请输入数量'
			},{
				id:'packingRemark',
				fieldLabel : '备注',
				name : 'packingItem.remark'
			}],
			buttons : [button_add,button_clear,button_del]
		
	});
		return packingsForm;
	}
  }
}();








